package com.wxxymakers.cklabrecruit.service;

import com.wxxymakers.cklabrecruit.entity.EmailRecord;
import com.wxxymakers.cklabrecruit.entity.EmailRecordExample;
import com.wxxymakers.cklabrecruit.entity.EmailTemplate;
import com.wxxymakers.cklabrecruit.entity.EmailTemplateExample;
import com.wxxymakers.cklabrecruit.mapper.EmailRecordMapper;
import com.wxxymakers.cklabrecruit.mapper.EmailTemplateMapper;
import com.wxxymakers.cklabrecruit.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/11/24 1:13
 * @DESCRIBE 邮件模板业务层
 */
@Service
public class EmailTemplateService implements IService<EmailTemplate> {

    private final EmailRecordMapper recordMapper;

    private final EmailTemplateMapper mapper;

    private final EmailReturnService emailReturnService;

    @Autowired
    public EmailTemplateService(EmailTemplateMapper mapper, EmailRecordMapper recordMapper, EmailReturnService emailReturnService) {
        this.mapper = mapper;
        this.recordMapper = recordMapper;
        this.emailReturnService = emailReturnService;
    }

    public EmailRecord getRecord(Integer rid) {
        return recordMapper.selectByPrimaryKey(rid);
    }

    public Page<EmailRecord> page(int pageNo, int size, EmailRecord record) {
        if (pageNo <= 0) pageNo = 1;
        EmailRecordExample example = new EmailRecordExample();
        example.setLimit(size);
        example.setOffset((pageNo - 1) * size);
        example.setOrderByClause("id desc");
        if (record != null) {
            EmailRecordExample.Criteria criteria = example.createCriteria();
            if (record.getStatus() != null) {
                criteria.andStatusEqualTo(String.valueOf(record.getStatus().ordinal()));
            }
            if (!StringUtils.isEmpty(record.getTitle())) {
                criteria.andTitleEqualTo(record.getTitle());
            }
        }
        List<EmailRecord> templates = recordMapper.selectByExampleWithBLOBs(example);
        if (!CollectionUtils.isEmpty(templates)) {
            long count = recordMapper.countByExample(example);
            return PageUtils.page(templates, pageNo, size, count);
        }
        return null;
    }

    @Transactional
    public EmailRecord addRecord(EmailRecord record) {
        record.setDatetime(new Date());
        if (recordMapper.insert(record) > 0) {
            return record;
        }
        return null;
    }

    @Transactional
    public boolean deleteRecord(Integer id) {
        return recordMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 清空邮件发送记录 同时清空反馈
     */
    @Transactional
    public boolean clear() {
        List<EmailRecord> emailRecords = recordMapper.selectByExample(null);
        if (!CollectionUtils.isEmpty(emailRecords)) {
            Integer[] integers = emailRecords.stream().map(EmailRecord::getId).toArray(Integer[]::new);
            if (emailReturnService.deleteByRecordId(integers)) {
                return recordMapper.deleteByExample(new EmailRecordExample()) > 0;
            }
        }
        return false;
    }

    @Transactional
    public EmailTemplate saveAndUpdate(EmailTemplate entity) {
        entity.setDatetime(new Date());
        EmailTemplate emailTemplate = mapper.selectByTitle(entity.getTitle().trim());
        if (emailTemplate != null) {
            entity.setId(emailTemplate.getId());
            if (this.change(entity)) {
                return entity;
            }
        }
        return this.add(entity);
    }

    @Override
    @Transactional
    public EmailTemplate add(EmailTemplate entity) {
        entity.setDatetime(new Date());
        if (mapper.insertSelective(entity) > 0) {
            return entity;
        }
        return null;
    }

    @Override
    @Transactional
    public EmailTemplate delete(Object id) {
        EmailTemplate one = this.getOne(id);
        if (one != null) {
            if (mapper.deleteByPrimaryKey((Integer) id) > 0) {
                return one;
            }
        }
        return one;
    }

    @Override
    @Transactional
    public boolean change(EmailTemplate entity) {
        return mapper.updateByPrimaryKeySelective(entity) > 0;
    }

    @Override
    public EmailTemplate getOne(Object id) {
        return mapper.selectByPrimaryKey((Integer) id);
    }

    @Override
    public Page<EmailTemplate> getPage(int pageNo, int size, EmailTemplate entity) {
        if (pageNo <= 0) pageNo = 1;
        EmailTemplateExample example = new EmailTemplateExample();
        example.setLimit(size);
        example.setOffset((pageNo - 1) * size);
        example.setOrderByClause("id desc");
        if (entity != null) {
            EmailTemplateExample.Criteria criteria = example.createCriteria();
            if (!StringUtils.isEmpty(entity.getTitle())) {
                criteria.andTitleEqualTo(entity.getTitle());
            }
            if (!StringUtils.isEmpty(entity.getSendFrom())) {
                criteria.andSendFromEqualTo(entity.getSendFrom());
            }
        }
        List<EmailTemplate> templates = mapper.selectByExampleWithBLOBs(example);
        if (!CollectionUtils.isEmpty(templates)) {
            long count = mapper.countByExample(example);
            return PageUtils.page(templates, pageNo, size, count);
        }
        return null;
    }
}
